package com.flower.service.impl;

import com.flower.mapper.CustomerMapper;
import com.flower.model.dto.CustomerDTO;
import com.flower.model.dto.CustomerPageQueryDTO;
import com.flower.model.entity.Customer;
import com.flower.result.PageResult;
import com.flower.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("all")
/**
 *@ClassName CustomerServiceImpl
 *@Description TODO
 *@Author @O_o  GW__
 *@Date 2025/6/13 15:35
 *@Version 1.0
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult page(CustomerPageQueryDTO customerPageQueryDTO) {
        PageHelper.startPage(customerPageQueryDTO.getPageNum(), customerPageQueryDTO.getPageSize());
        Page<Customer> page = customerMapper.pageQuery(customerPageQueryDTO);
        long total = page.getTotal();
        List<Customer> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void addOrUpdate(CustomerDTO customerDTO) {
        if (customerDTO.getId() == null) {
            customerMapper.insertCustomer(customerDTO);
        } else {
            customerMapper.updateCustomer(customerDTO);
        }
    }

    @Override
    public void deleteCustomer(Integer id) {
        customerMapper.deleteCustomerById(id);
    }

    @Override
    public void deleteCustomerBatch(List<Integer> ids) {
        ids.forEach(id -> customerMapper.deleteCustomerById(id));
    }

    @Override
    public List<Customer> list() {
        return customerMapper.getAllCustomers();
    }

    @Override
    public int nameValidate() {
        Integer Validate;

        Validate = customerMapper.userNameValidate();

        if (Validate == null)
            return 0;
        return Validate.intValue();
    }

    @Override
    public int saleValidate() {
        Integer Validate ;

        Validate = customerMapper.salerValidate();

        if(Validate == null)
            return 0;
        return Validate.intValue();
    }

    @Override
    public void saveBatch(List<Customer> customers) {
        customerMapper.saveBatchAllData(customers);
    }
}
